package com.madrivergroup.mamatodo.util;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;

public class MaMaUtil implements Serializable {
	
	public static final String PATTERN = "MM/dd/yyyy";
		
	/**
	 * Converts the incoming Date into a String
	 * @param date
	 * @return
	 */
	public static String convertToString( Date date ) {

		if( null == date ) {
			date = Calendar.getInstance().getTime();
		}
		String formattedDate = null;
		try {
			DateFormat format = new SimpleDateFormat(PATTERN);
			formattedDate = format.format( date );
		}
		catch( Exception ex ) {
			Log.e("MaMaUtil:convertToString()", ex.getMessage() );
			formattedDate = "??";
		}
		
		return formattedDate;
	} // end of convertToString()
	
	/**
	 * Converts incoming String to the Date
	 * @param dt
	 * @return
	 */
	public static Date convertToDate( String dt ) {
		
		Date date = null;
		try {
			SimpleDateFormat format = new SimpleDateFormat();
			format.setLenient(false);
			format.applyPattern(PATTERN);
			date = format.parse( dt );
		}
		catch( Exception ex ) {
			Log.e("MaMaUtil:convertToString()", ex.getMessage() );
			date = Calendar.getInstance().getTime();		}

		
		return date;
	}

}
